<template>
  <div class="personalized-recommendations">
    <div v-if="!isUserLoggedIn" class="login-prompt">
      <p>登录后查看您的个性化推荐</p>
      <el-button type="primary" size="small" @click="goToLogin">立即登录</el-button>
    </div>

    <template v-else>
      <div class="recommendations-header">
        <h3>为您推荐</h3>
        <p v-if="favoriteTags && favoriteTags.length > 0" class="favorite-tags">
          根据您对
          <span v-for="(tag, index) in favoriteTags" :key="tag.id" class="tag">
            {{ tag.name }}{{ index < favoriteTags.length - 1 ? '、' : '' }}
          </span>
          的兴趣推荐
        </p>
      </div>

      <div v-if="loading" class="loading-container">
        <el-skeleton :rows="3" animated />
      </div>

      <div v-else-if="recommendations && recommendations.length > 0" class="recommendations-grid">
        <div 
          v-for="article in recommendations" 
          :key="article.id" 
          class="recommendation-item"
          @click="goToArticle(article.id)"
        >
          <div class="article-cover">
            <img :src="article.cover || defaultCover" :alt="article.title" />
          </div>
          <div class="article-info">
            <h4 class="article-title">{{ article.title }}</h4>
            <div class="article-meta">
              <span class="article-category">{{ article.categoryName }}</span>
              <span class="article-time">{{ formatDate(article.createTime) }}</span>
            </div>
          </div>
        </div>
      </div>

      <div v-else class="no-recommendations">
        <el-empty description="暂无推荐内容" :image-size="80"></el-empty>
      </div>
    </template>
  </div>
</template>

<script>
import { getPersonalizedRecommendationsApi } from '@/api/recommendation';
import { formatDate } from '@/utils/date';
import { isLogin } from '@/utils/auth';

export default {
  name: 'PersonalizedRecommendations',
  data() {
    return {
      loading: false,
      recommendations: [],
      favoriteTags: [],
      favoriteCategories: [],
      isUserLoggedIn: false,
      defaultCover: 'https://via.placeholder.com/300x200/f0f0f0/cccccc?text=暂无封面'
    };
  },
  methods: {
    formatDate,
    goToArticle(id) {
      this.$router.push(`/post/${id}`);
    },
    goToLogin() {
      this.$router.push('/login');
    },
    checkLoginStatus() {
      this.isUserLoggedIn = isLogin();
    },
    fetchRecommendations() {
      if (!this.isUserLoggedIn) {
        return;
      }
      
      this.loading = true;
      getPersonalizedRecommendationsApi()
        .then(res => {
          if (res.code === 200 && res.data) {
            this.recommendations = res.data.recommendations || [];
            this.favoriteTags = res.data.favoriteTags || [];
            this.favoriteCategories = res.data.favoriteCategories || [];
          }
        })
        .catch(error => {
          console.error('获取个性化推荐失败:', error);
          // 显示更友好的错误消息，如有需要
        })
        .finally(() => {
          this.loading = false;
        });
    }
  },
  created() {
    this.checkLoginStatus();
    this.fetchRecommendations();
  }
};
</script>

<style lang="scss" scoped>
.personalized-recommendations {
  margin-bottom: 32px;
  padding: 24px;
  background-color: var(--bg-secondary);
  border-radius: 10px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.login-prompt {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
  text-align: center;
  
  p {
    margin-bottom: 12px;
    color: var(--text-secondary);
  }
}

.recommendations-header {
  margin-bottom: 24px;
  
  h3 {
    font-size: 1.3rem;
    font-weight: 600;
    margin-bottom: 8px;
    color: var(--text-primary);
  }

  .favorite-tags {
    font-size: 0.9rem;
    color: var(--text-secondary);

    .tag {
      color: var(--primary);
      font-weight: 500;
    }
  }
}

.recommendations-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  
  @media (max-width: 768px) {
    grid-template-columns: repeat(2, 1fr);
  }
  
  @media (max-width: 640px) {
    grid-template-columns: 1fr;
  }
}

.recommendation-item {
  cursor: pointer;
  overflow: hidden;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  border-radius: 8px;
  background: var(--bg-primary);
  
  &:hover {
    transform: translateY(-3px);
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  }
  
  .article-cover {
    height: 140px;
    width: 100%;
    overflow: hidden;
    
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      transition: transform 0.3s ease;
    }
  }
  
  &:hover .article-cover img {
    transform: scale(1.05);
  }
  
  .article-info {
    padding: 4px;
  }
  
  .article-title {
    margin: 0 0 8px;
    font-size: 1rem;
    font-weight: 500;
    color: var(--text-primary);
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    line-height: 1.4;
  }
  
  .article-meta {
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
    color: var(--text-tertiary);
  }
}

.loading-container {
  padding: 16px;
}

.no-recommendations {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 32px;
}
</style> 